/********************************************************************
	Copyright (c) 2010-2020 by TnmuJoy
	@created:	17-10-2011   21:45
	@filename: 	State.hxx
	@author:	limiko lau(limiko.lau@gmail.com)
	@desc:		
*********************************************************************/
#ifndef TNMUJOY_GDUT_DESIGNPATTERN_STATE_H_
#define TNMUJOY_GDUT_DESIGNPATTERN_STATE_H_

namespace tnmujoy
{
namespace gdut
{

class StateContext;

class State
{
protected:
    friend class StateContext;
    State(){}
    virtual ~State(){}
    virtual void run() = 0;
    virtual void changeState(StateContext* _stateContext) = 0;
    void changeState(StateContext* _stateContext, State* _nextState)
    {
        _stateContext->changeState(_nextState);
    }
};

class StateContext
{
public:
    StateContext(State* _state)
        : state_(_state)
    {

    }
    ~StateContext()
    {
        if (state_)
        {
            delete state_;
        }
    }
    void changeState()
    {
        state_->run();
        state_->changeState(this);
    }
protected:
private:
    friend class State;
    void changeState(State* _state)
    {
        this->state_ = _state;
    }
    State* state_;
};

}//namespace gdut
}//namespace tnmujoy

#endif //TNMUJOY_GDUT_DESIGNPATTERN_STATE_H_